package com.github.llyb120.performance.test;

import groovy.lang.Closure;
import java.util.*;
import static com.github.llyb120.performance.define.Helper.*;
import static com.github.llyb120.performance.define.Helper.EventType.POST;
import static com.github.llyb120.performance.define.Helper.EventType.PRE;


public class All {


    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };
    private final static ThreadLocal<LinkedList<String>> localPath = new ThreadLocal(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };

    public static void generate (Closure generate){
        pushPathStack("generate");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Generate _generate = new Generate();

        pushStack(_generate);
        if(generate != null){
            generate .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitGenerate(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_generate);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void fileName (String fileName){
        pushPathStack("fileName");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).setFileName (fileName);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void num (int num){
        pushPathStack("num");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).setNum (num);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (String label){
        field (label,(Closure) null );
    }
    public static void field (String label,Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        _field .setLabel (label);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (String label,String generator){
        field (label,generator,(Closure) null );
    }
    public static void field (String label,String generator,Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        _field .setLabel (label);
        _field .setGenerator (generator);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (String label,String generator,Map params){
        field (label,generator,params,(Closure) null );
    }
    public static void field (String label,String generator,Map params,Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        _field .setLabel (label);
        _field .setGenerator (generator);
        _field .setParams (params);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generate) {
            ((Generate)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void generator (String generator){
        pushPathStack("generator");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setGenerator (generator);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void label (String label){
        pushPathStack("label");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setLabel (label);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void params (Map params){
        pushPathStack("params");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setParams (params);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }



    private static Object pickStack(){
        return local.get().getLast();
    }
    private static void pushStack(Object obj){
        local.get().addLast(obj);
    }
    private static Object popStack(){
        return local.get().removeLast();
    }
    private static void pushPathStack(String name){
        localPath.get().addLast((name));
    }
    private static Object popPathStack(){
        return localPath.get().removeLast();
    }
    private static String getCurrentPath(){
        return String.join(".", localPath.get());
    }

}